<html>
<head><meta charset="utf-8"><title>Understanding `mir::TerminatorKind::Call` · t-compiler/wg-mir-opt · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/index.html">t-compiler/wg-mir-opt</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Understanding.20.60mir.3A.3ATerminatorKind.3A.3ACall.60.html">Understanding `mir::TerminatorKind::Call`</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="234657747"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Understanding%20%60mir%3A%3ATerminatorKind%3A%3ACall%60/near/234657747" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hilmar Gústafsson <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Understanding.20.60mir.3A.3ATerminatorKind.3A.3ACall.60.html#234657747">(Apr 15 2021 at 11:27)</a>:</h4>
<p>I'm working on taint analysis, and am struggling to understand how the <code>mir::TerminatorKind::Call</code> specifies a _function_ using <code>mir::Operand</code>. Does anyone know where I could see this being done in the compiler?</p>



<a name="234657906"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Understanding%20%60mir%3A%3ATerminatorKind%3A%3ACall%60/near/234657906" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Understanding.20.60mir.3A.3ATerminatorKind.3A.3ACall.60.html#234657906">(Apr 15 2021 at 11:28)</a>:</h4>
<p>For direct calls the type of the operand is an <code>FnDef</code> corresponding to the respective function. For indirect calls it is an <code>FnPtr</code>.</p>



<a name="234658607"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Understanding%20%60mir%3A%3ATerminatorKind%3A%3ACall%60/near/234658607" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hilmar Gústafsson <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Understanding.20.60mir.3A.3ATerminatorKind.3A.3ACall.60.html#234658607">(Apr 15 2021 at 11:34)</a>:</h4>
<p><span class="user-mention silent" data-user-id="133247">bjorn3</span> <a href="#narrow/stream/189540-t-compiler.2Fwg-mir-opt/topic/Understanding.20.60mir.3A.3ATerminatorKind.3A.3ACall.60/near/234657906">said</a>:</p>
<blockquote>
<p>For direct calls the type of the operand is an <code>FnDef</code> corresponding to the respective function. For indirect calls it is an <code>FnPtr</code>.</p>
</blockquote>
<p>Thanks!</p>



<a name="234659284"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Understanding%20%60mir%3A%3ATerminatorKind%3A%3ACall%60/near/234659284" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hilmar Gústafsson <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Understanding.20.60mir.3A.3ATerminatorKind.3A.3ACall.60.html#234659284">(Apr 15 2021 at 11:40)</a>:</h4>
<p>Yep. That's exactly what I'm looking for. :D</p>



<a name="234827431"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Understanding%20%60mir%3A%3ATerminatorKind%3A%3ACall%60/near/234827431" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hilmar Gústafsson <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Understanding.20.60mir.3A.3ATerminatorKind.3A.3ACall.60.html#234827431">(Apr 16 2021 at 10:09)</a>:</h4>
<p>Does anyone know how I would go about constructing this artificially? Right now my best bet is this:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="n">kind</span>: <span class="nc">mir</span>::<span class="n">TerminatorKind</span>::<span class="n">Call</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="n">func</span>: <span class="nc">mir</span>::<span class="n">Operand</span>::<span class="n">Constant</span><span class="p">(</span><span class="nb">Box</span>::<span class="n">new</span><span class="p">(</span><span class="n">mir</span>::<span class="n">Constant</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="n">span</span>: <span class="nc">DUMMY_SP</span><span class="p">,</span><span class="w"></span>
<span class="w">        </span><span class="n">user_ty</span>: <span class="nb">None</span><span class="p">,</span><span class="w"></span>
<span class="w">        </span><span class="n">literal</span>: <span class="nc">mir</span>::<span class="n">ConstantKind</span>::<span class="n">Ty</span><span class="p">(</span><span class="o">&amp;</span><span class="n">ty</span>::<span class="n">Const</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">            </span><span class="n">ty</span>: <span class="kp">&amp;</span><span class="nc">ty</span>::<span class="n">TyS</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">                </span><span class="n">kind</span>: <span class="nc">ty</span>::<span class="n">FnDef</span><span class="p">(</span><span class="n">input_fn</span><span class="p">.</span><span class="n">to_def_id</span><span class="p">(),</span><span class="w"> </span><span class="n">ty</span>::<span class="n">List</span>::<span class="n">empty</span><span class="p">()),</span><span class="w"></span>
<span class="w">                </span><span class="n">flags</span>: <span class="nc">TypeFlags</span>::<span class="n">empty</span><span class="p">(),</span><span class="w"> </span><span class="sd">/// ???</span>
<span class="w">                </span><span class="n">outer_exclusive_binder</span>: <span class="nc">DebruijnIndex</span>::<span class="n">MAX</span><span class="p">,</span><span class="w"> </span><span class="c1">// ???</span>
<span class="w">            </span><span class="p">},</span><span class="w"></span>
<span class="w">            </span><span class="n">val</span>: <span class="o">???</span><span class="p">,</span><span class="w"></span>
<span class="w">        </span><span class="p">}),</span><span class="w"></span>
<span class="w">    </span><span class="p">})),</span><span class="w"></span>
<span class="w">    </span><span class="n">args</span>: <span class="nc">vec</span><span class="o">!</span><span class="p">[],</span><span class="w"></span>
<span class="w">    </span><span class="n">destination</span>: <span class="nb">Some</span><span class="p">((</span><span class="w"></span>
<span class="w">        </span><span class="n">mir</span>::<span class="n">Place</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">            </span><span class="n">local</span>: <span class="nc">mir</span>::<span class="n">Local</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">private</span>: <span class="mi">1</span><span class="w"> </span><span class="p">},</span><span class="w"></span>
<span class="w">            </span><span class="n">projection</span>: <span class="nc">ty</span>::<span class="n">List</span>::<span class="n">empty</span><span class="p">(),</span><span class="w"></span>
<span class="w">        </span><span class="p">},</span><span class="w"></span>
<span class="w">        </span><span class="n">mir</span>::<span class="n">START_BLOCK</span><span class="p">,</span><span class="w"></span>
<span class="w">    </span><span class="p">)),</span><span class="w"></span>
<span class="w">    </span><span class="n">cleanup</span>: <span class="nb">None</span><span class="p">,</span><span class="w"></span>
<span class="w">    </span><span class="n">from_hir_call</span>: <span class="nc">false</span><span class="p">,</span><span class="w"></span>
<span class="w">    </span><span class="n">fn_span</span>: <span class="nc">DUMMY_SP</span><span class="p">,</span><span class="w"></span>
<span class="p">},</span><span class="w"></span>
</code></pre></div>



<a name="234827766"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Understanding%20%60mir%3A%3ATerminatorKind%3A%3ACall%60/near/234827766" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Understanding.20.60mir.3A.3ATerminatorKind.3A.3ACall.60.html#234827766">(Apr 16 2021 at 10:12)</a>:</h4>
<p>You need to use <a href="https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/struct.TyCtxt.html#method.mk_fn_def"><code>tcx.mk_fn_def</code></a> instead of manually constructing the type.</p>



<a name="234827819"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Understanding%20%60mir%3A%3ATerminatorKind%3A%3ACall%60/near/234827819" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Understanding.20.60mir.3A.3ATerminatorKind.3A.3ACall.60.html#234827819">(Apr 16 2021 at 10:13)</a>:</h4>
<p>And to get from <code>ty::Const</code> to <code>&amp;'tcx ty::Const</code> you need to use <code>tcx.mk_const</code>.</p>



<a name="234828026"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Understanding%20%60mir%3A%3ATerminatorKind%3A%3ACall%60/near/234828026" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Understanding.20.60mir.3A.3ATerminatorKind.3A.3ACall.60.html#234828026">(Apr 16 2021 at 10:14)</a>:</h4>
<p>Why do you need to make it yourself by the way?</p>



<a name="234828055"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Understanding%20%60mir%3A%3ATerminatorKind%3A%3ACall%60/near/234828055" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hilmar Gústafsson <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Understanding.20.60mir.3A.3ATerminatorKind.3A.3ACall.60.html#234828055">(Apr 16 2021 at 10:15)</a>:</h4>
<p>Thank you! Oh, I'm just writing a test case for some taint analysis</p>



<a name="234828135"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Understanding%20%60mir%3A%3ATerminatorKind%3A%3ACall%60/near/234828135" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hilmar Gústafsson <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Understanding.20.60mir.3A.3ATerminatorKind.3A.3ACall.60.html#234828135">(Apr 16 2021 at 10:15)</a>:</h4>
<p>It's for our master's thesis, and we're using a simplified MIR, so we need to construct it artificially for now</p>



<a name="234828148"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Understanding%20%60mir%3A%3ATerminatorKind%3A%3ACall%60/near/234828148" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Understanding.20.60mir.3A.3ATerminatorKind.3A.3ACall.60.html#234828148">(Apr 16 2021 at 10:15)</a>:</h4>
<p>MIR is not really meant to be manually constructed. Even miri's test suite just compiles regular rust code.</p>



<a name="234828227"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Understanding%20%60mir%3A%3ATerminatorKind%3A%3ACall%60/near/234828227" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Understanding.20.60mir.3A.3ATerminatorKind.3A.3ACall.60.html#234828227">(Apr 16 2021 at 10:16)</a>:</h4>
<p><a href="https://github.com/rust-lang/miri/issues/196">https://github.com/rust-lang/miri/issues/196</a></p>



<a name="234829538"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Understanding%20%60mir%3A%3ATerminatorKind%3A%3ACall%60/near/234829538" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hilmar Gústafsson <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Understanding.20.60mir.3A.3ATerminatorKind.3A.3ACall.60.html#234829538">(Apr 16 2021 at 10:26)</a>:</h4>
<p>I see... but it kinda seems like it wouldn't be too bad if we implemented a MIR parser at some point?</p>



<a name="234829609"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Understanding%20%60mir%3A%3ATerminatorKind%3A%3ACall%60/near/234829609" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hilmar Gústafsson <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Understanding.20.60mir.3A.3ATerminatorKind.3A.3ACall.60.html#234829609">(Apr 16 2021 at 10:27)</a>:</h4>
<p>In any case, I figured this was the easiest way to get exactly the MIR that we wanted. I'm not even sure we can construct the simplified MIR using valid Rust code</p>



<a name="234829767"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Understanding%20%60mir%3A%3ATerminatorKind%3A%3ACall%60/near/234829767" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hilmar Gústafsson <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Understanding.20.60mir.3A.3ATerminatorKind.3A.3ACall.60.html#234829767">(Apr 16 2021 at 10:28)</a>:</h4>
<p>But we _will_ be using real Rust code for the final version of the taint analysis, just not this intermediate simplified step</p>



<a name="234830397"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Understanding%20%60mir%3A%3ATerminatorKind%3A%3ACall%60/near/234830397" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hilmar Gústafsson <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Understanding.20.60mir.3A.3ATerminatorKind.3A.3ACall.60.html#234830397">(Apr 16 2021 at 10:33)</a>:</h4>
<p>Nevermind. I just saw <code>ty::GlobalCtxt</code>...</p>



<a name="234831316"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Understanding%20%60mir%3A%3ATerminatorKind%3A%3ACall%60/near/234831316" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Understanding.20.60mir.3A.3ATerminatorKind.3A.3ACall.60.html#234831316">(Apr 16 2021 at 10:40)</a>:</h4>
<p>There is desire to be able to parse some form of MIR for testing, but I don't remember the plan and I don't think I ever properly wrote down <span class="user-mention" data-user-id="119009">@eddyb</span> 's plan. I think the miri repo has an issue open about it. There may also be an issue in the rustc repo</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>